package com.devframe.component.ftlprocess.engine.impl;

import java.io.Writer;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



import com.devframe.component.ftlprocess.FTLProcessGlobalConstants;
import com.devframe.component.ftlprocess.MyFTLProcessHandleBean;
import com.devframe.component.ftlprocess.engine.AbstractFTLProcessEngine;
import com.devframe.component.ftlprocess.engine.MyFTLProcessEngine;
import com.devframe.component.ftlprocess.meta.AskData;
import com.devframe.component.ftlprocess.meta.DataSet;
import com.devframe.component.ftlprocess.meta.Model;



public class MyFTLProcessDefaultEngineImpl extends AbstractFTLProcessEngine implements MyFTLProcessEngine {
	
	/**
	 * 执行SQL
	 * @param baseDao
	 * @param clientParams
	 * @param sql
	 * @return
	 * @author:詹保山
	 */
	private List execSQL(Map<String,Object> clientParams,String sql){
		List ls=null;
		if(clientParams!=null){
			ls=super.getBaseDao().sqlQueryForList(sql, clientParams);
		}else{
			ls=super.getBaseDao().sqlQueryForList(sql);
		}
		return ls;
	}
	private List execHQL(Map<String,Object> clientParams,String sql){
		List ls=null;
		if(clientParams!=null){
			ls=super.getBaseDao().queryForList(sql, clientParams);
		}else{
			ls=super.getBaseDao().queryForList(sql);
		}
		return ls;
	}
	
	
	private Object execDataSet(DataSet ds,Map<String, Object> clientParams){
		Object result=null;
		if(ds.getType()==FTLProcessGlobalConstants.DATASET_TYPE_SQL){//SQL的执行方式
			List ls=this.execSQL(clientParams, ds.getSql());
		    result=this.convertResult(ls);
		}else if(ds.getType()==FTLProcessGlobalConstants.DATASET_TYPE_BEAN){//Service执行方式
			result=this.execServiceBean(ds, clientParams);
		}else if(ds.getType()==FTLProcessGlobalConstants.DATASET_TYPE_HQL){//HQL 执行方式
			List ls=this.execHQL(clientParams, ds.getSql());
		    result=this.convertResult(ls);
		}
		return result;
	}
  

	
	private void desplayXml(AskData askData ,Writer writer){
		String tmplFile=askData.getTemplateFile();
		
		MyFTLProcessHandleBean handleBean=this.accessHandleBean(tmplFile);
		//获取参数
		Map<String,Object> clientParams=askData.getAllParams();
		Map<String,Object> root=new HashMap<String, Object>();
		
		//压入所有参数
		root.putAll(askData.getAllParams());
		
//		Report report=new Report();
//		if(logger.isDebugEnabled()){
//			logger.debug("【root="+root+"】,report=【"+report+"】");
//		}
		Model model=this.accessModel(tmplFile,handleBean,clientParams);
		if(logger.isDebugEnabled()){
			logger.debug("model={"+model+"}");
		}
//		logger.info("开始解析datasets");
		Object result;
		//获取数据集
		for(DataSet ds:model.getDatasets()){
			result=this.execDataSet(ds,clientParams);
			root.put(ds.getId(), result);
		}
//		logger.info("datasets解析完毕");
		
		root.put(FTLProcessGlobalConstants.ASK_DATA_KEY, askData);
		//结果集的过滤器
		this.renderNodeProcess(handleBean, root, writer);
//		model.destory();
		root.clear();
		root=null;
	}
	
	public void execEngine(AskData askData ,Writer writer) {
		this.desplayXml(askData, writer);
	}

 


	
	
}
